package com.robusta.cityuate;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.google.android.gcm.GCMBaseIntentService;
import com.google.android.gcm.GCMConstants;
import com.google.android.gcm.GCMRegistrar;

/**
 * This class is started up as a service of the Android application. It listens
 * for Google Cloud Messaging (GCM) messages directed to this device.
 * 
 * When the device is successfully registered for GCM, a message is sent to the
 * App Engine backend via Cloud Endpoints, indicating that it wants to receive
 * broadcast messages from the it.
 * 
 * Before registering for GCM, you have to create a project in Google's Cloud
 * Console (https://code.google.com/apis/console). In this project, you'll have
 * to enable the "Google Cloud Messaging for Android" Service.
 * 
 * Once you have set up a project and enabled GCM, you'll have to set the
 * PROJECT_NUMBER field to the project number mentioned in the "Overview" page.
 * 
 * See the documentation at
 * http://developers.google.com/eclipse/docs/cloud_endpoints for more
 * information.
 */
public class GCMIntentService extends GCMBaseIntentService {

	protected static final String PROJECT_NUMBER = "17396208190";
	private static String TAG = GCMIntentService.class.getName();

	public GCMIntentService() {

		super(PROJECT_NUMBER);
		Log.v(TAG, "GCMIntentService public constructor");
	}

	/**
	 * Register the device for GCM.
	 * 
	 * @param mContext
	 *            the activity's context.
	 */
	public static void register(Context mContext) {
		GCMRegistrar.checkDevice(mContext);
		GCMRegistrar.checkManifest(mContext);
		final String regId = GCMRegistrar.getRegistrationId(mContext);

		if (regId.equals("")) {
			GCMRegistrar.register(mContext, PROJECT_NUMBER);
			Log.v(TAG, "registering");

		} else {
			Log.v(TAG, "Already registered");
		}
	}

	/**
	 * Unregister the device from the GCM service.
	 * 
	 * @param mContext
	 *            the activity's context.
	 */
	public static void unregister(Context mContext) {
		GCMRegistrar.unregister(mContext);
		Log.v(TAG, "unregistering");
	}

	/**
	 * Called on registration error. This is called in the context of a Service
	 * - no dialog or UI.
	 * 
	 * @param context
	 *            the Context
	 * @param errorId
	 *            an error message
	 */
	@Override
	public void onError(Context context, String errorId) {
		Log.v(TAG, "error, id: " + errorId);

		if (errorId.equals(GCMConstants.ERROR_AUTHENTICATION_FAILED)) {
			NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
			// Prepare intent which is triggered if the
			// notification is selected

			Intent intent = new Intent("android.settings.SYNC_SETTINGS");
			PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
					0);

			Notification noti = new NotificationCompat.Builder(this)
					.setContentTitle("Check Google Account")
					.setContentText("Bad Google Account Password")
					.setSmallIcon(R.drawable.abs__ab_bottom_solid_dark_holo)
					.setContentIntent(pIntent).build();

			// Hide the notification after its selected
			noti.flags |= Notification.FLAG_AUTO_CANCEL;

			notificationManager.notify(0, noti);
		} else if (errorId.equals(GCMConstants.ERROR_ACCOUNT_MISSING)) {
			NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
			// Prepare intent which is triggered if the
			// notification is selected

			Intent intent = new Intent("android.settings.SYNC_SETTINGS");
			PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
					0);

			// Build notification
			// Actions are just fake
			Notification noti = new NotificationCompat.Builder(this)
					.setContentTitle("Add Google Account")
					.setContentText("No Google Account on Phone")
					.setSmallIcon(R.drawable.abs__ab_bottom_solid_dark_holo)
					.setContentIntent(pIntent).build();

			// Hide the notification after its selected
			noti.flags |= Notification.FLAG_AUTO_CANCEL;

			notificationManager.notify(0, noti);
		}

	}

	/**
	 * Called when a cloud message has been received.
	 */
	@Override
	public void onMessage(Context context, Intent intent) {
		Log.v(TAG, "message received");
		// Handle new Notification
	}

	/**
	 * Called back when a registration token has been received from the Google
	 * Cloud Messaging service.
	 * 
	 * @param context
	 *            the Context
	 */
	@Override
	public void onRegistered(Context context, String registration) {
		Log.v(TAG, "Registered, id: " + registration);
		// Send new Registration id to Server
	}

	/**
	 * Called back when the Google Cloud Messaging service has unregistered the
	 * device.
	 * 
	 * @param context
	 *            the Context
	 */
	@Override
	protected void onUnregistered(Context context, String registrationId) {
		Log.v(TAG, "Unregistered, id: " + registrationId);
		// Remove Registration id from Server

	}

}
